﻿Imports System.IO
Imports System.Xml.Serialization

NotInheritable Class FileHandler

    Public Shared ReadOnly SETTINGSPATH As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Sansa Image Converter")
    Shared ReadOnly SETTINGSFILE As String = Path.Combine(SETTINGSPATH, "Settings.xml")

    Public Shared Sub SaveOptions(ByVal appSettings As AppSettings)
        If Not File.Exists(SETTINGSPATH) Then
            Directory.CreateDirectory(SETTINGSPATH)
        End If
        Dim formatter As New XmlSerializer(GetType(AppSettings))
        Using stream As New StreamWriter(SETTINGSFILE, False, System.Text.Encoding.UTF8)
            formatter.Serialize(stream, appSettings)
        End Using
    End Sub

    Public Shared Sub LoadOptions(ByRef appSettings As AppSettings)
        If Not File.Exists(SETTINGSPATH) Then
            Directory.CreateDirectory(SETTINGSPATH)
            appSettings = New AppSettings
        End If
        If File.Exists(SETTINGSFILE) Then
            Dim formatter As New XmlSerializer(GetType(AppSettings))
            Using stream As New StreamReader(SETTINGSFILE)
                appSettings = DirectCast(formatter.Deserialize(stream), AppSettings)
            End Using
        Else
            appSettings = New AppSettings
        End If
    End Sub

End Class
